package com.example.demo.controller;

import com.example.demo.entity.ApiResponse;
import com.example.demo.entity.Cart;
import com.example.demo.service.CartService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {
    private final static Logger logger = LoggerFactory.getLogger(CartController.class);
    @Autowired
    private CartService cartService;

    @PreAuthorize("hasAuthority('cart:read')")
    @GetMapping("/cart/{user_id}")
    public ResponseEntity<ApiResponse> getCart(@PathVariable("user_id") int user_id) {
        ApiResponse apiResponse;
        List<Cart> list;
        try {
            list = cartService.getList(user_id);
            if (list != null) {
                apiResponse = new ApiResponse(1, "数据查询成功", list);
            } else {
                apiResponse = new ApiResponse(0, "数据查询失败", null);
            }
            logger.info("id:{}", user_id);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            logger.error("id：{}", user_id);
            logger.error(e.getMessage());
            apiResponse = new ApiResponse(2, "数据查询失败", null);
            return ResponseEntity.status(500).body(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('cart:create')")
    @PostMapping("/cart/add/{bookId}")
    public ResponseEntity<ApiResponse> addCart(@PathVariable("bookId") int bookId, @RequestBody Cart cart) {
        ApiResponse apiResponse;
        try {
            cart.setBookId(bookId);
            int line = cartService.addCart(cart);
            if (line > 0) {
                apiResponse = new ApiResponse(1, "数据添加成功", null);
            } else {
                apiResponse = new ApiResponse(0, "数据添加失败", null);
            }
            logger.info("id:{},数据：{}", bookId, cart);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            logger.error("id:{},数据:{}", bookId, cart);
            logger.error(e.getMessage());
            apiResponse = new ApiResponse(2, "数据添加失败", null);
            return ResponseEntity.status(500).body(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('cart:update')")
    @PutMapping("/cart/update")
    public ResponseEntity<ApiResponse> updateCart(@RequestBody Cart cart) {
        ApiResponse apiResponse;
        try {
            int line = cartService.updateQuantity(cart);
            if (line > 0) {
                apiResponse = new ApiResponse(1, "数据修改成功", null);
            } else {
                apiResponse = new ApiResponse(0, "数据修改失败", null);
            }
            logger.info("数据：{}", cart);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            logger.error("数据：{}", cart);
            logger.error(e.getMessage());
            apiResponse = new ApiResponse(2, "数据修改失败", null);
            return ResponseEntity.status(500).body(apiResponse);
        }
    }

    @PreAuthorize("hasAuthority('cart:delete')")
    @DeleteMapping("/cart/clear/{user_id}")
    public ResponseEntity<ApiResponse> deleteCart(@PathVariable("user_id") Integer user_id) {
        ApiResponse apiResponse;
        try {
            int line = cartService.clearCart(user_id);
            if (line > 0) {
                apiResponse = new ApiResponse(1, "数据删除成功", null);
            } else {
                apiResponse = new ApiResponse(0, "数据删除失败", null);
            }
            logger.info("id:{}", user_id);
            return ResponseEntity.ok(apiResponse);
        } catch (Exception e) {
            logger.error("id：{}", user_id);
            logger.error(e.getMessage());
            apiResponse = new ApiResponse(2, "数据删除失败", null);
            return ResponseEntity.status(500).body(apiResponse);
        }
    }

}
